/*******************************************************************************
 * Package: com.song.boot.springstudy.thread.future01.demo
 * Type:    OrderService
 * Date:    2025-09-10 10:55
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.thread.future01.demo;

import lombok.SneakyThrows;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述： 订单 ---扣库存
 *
 * @author Songxianyang
 * @date 2025-09-10 10:55
 */
public class OrderFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        // 记录开始时间
        long begin = System.currentTimeMillis();

        // 下单
        System.out.println(threadPool.submit(() -> insertOrder()).get());
        // TimeUnit.SECONDS.sleep(2);
        // 减库存
        threadPool.submit(()->reduceInventory());

        // 记录结束时间
        long end = System.currentTimeMillis();

        // 4. 计算并打印耗时
        long costTime = end - begin;
        System.out.println("方法执行耗时：" + costTime + " 毫秒");
        threadPool.shutdown();
    }

    /**
     * 下单
     */
    @SneakyThrows
    private static String  insertOrder() {
        TimeUnit.SECONDS.sleep(2);
        return "下单成功2s";
    }

    /**
     * 减库存
     * reduceInventory
     */
    @SneakyThrows
    private static void reduceInventory() {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("减库存成功1s");
    }

}
